#!/bin/bash
#description: init debian system

set -e


command_exists() {
	command -v "$@" >/dev/null 2>&1
}


if [[ $UID != 0 ]]
then 
    echo "Fatal! Please run as root!" 
    exit 1
fi

b=$(expr substr "$(cat /etc/issue)" 1 12)
if [ "$b" = "Ubuntu 18.04" ]
then echo "">/dev/null
else 
    echo "请使用Ubuntu 18.04运行此脚本"
    exit 1
fi

echo "脚本开始运行..."
echo "正在安装阿里云镜像..."

if [ -d /etc/apt/sources.list ]
then 
    mv /etc/apt/sources.list /etc/apt/sources.list.bak
fi

sudo echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
">/etc/apt/sources.list

echo "正在更新安装源..."
sudo apt-get update>/dev/null
echo "OK"
echo "正在更新软件..."
sudo apt-get upgrade -y>/dev/null
echo "OK"

echo "正在安装配置Neovim"

command_exists nvim||{
	echo "正在安装Neovim"
	sudo apt-get install neovim -y>/dev/null
}
curl -fLo ~/.config/nvim/init.vim --create-dirs https://gitee.com/zimingzpp/nvim/raw/master/init.vim
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs https://gitee.com/zimingzpp/vim-plug/raw/master/plug.vim
command_exists node||{
	echo "安装nodejs"
 	sudo apt-get install nodejs -y>/dev/null
}
command_exists npm||{
 	echo "安装npm"
 	sudo apt-get install npm -y>/dev/null
}
 echo "设置npm安装源为淘宝源"
 npm config set registry https://registry.npm.taobao.org
echo "配置Neovim" 
nvim -c :PlugInstall -c :qa
cp -r ~/.local/share/nvim/site/plugged/gruvbox/colors ~/.local/share/nvim/site/
echo "安装已全部完成"

command_exists zsh||{
	echo "正在安装ZSH..."
	sudo apt-get install zsh -y>/dev/null
}
echo "将zsh设置为默认Shell"
sudo chsh -s /bin/zsh $USER

command_exists git||{
	echo "正在安装git..."
	sudo apt-get install git -y>/dev/null
}

echo "正在安装Ohmyzsh安装脚本"
#sh -c "$(wget -O- https://gitee.com/zimingzpp/ohmyzsh/raw/master/tools/install.sh>dev/null)"
if [ -d ~/.oh-my-zsh ]
then 
    rm -rf ~/.oh-my-zsh
    rm -rf ~/.zsh*
fi
wget  https://gitee.com/zimingzpp/ohmyzsh/raw/master/tools/install.sh
sed -i '286s/exec/#exec/g' ./install.sh
sed -i '172s/read\ opt/opt=y/g' ./install.sh
sh ./install.sh
rm ./install.sh*
echo "安装语法高亮插件"
git clone https://gitee.com/zimingzpp/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
chmod -R 755 ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
echo "安装自动补全插件"
git clone https://gitee.com/zimingzpp/zsh-autosuggestions.git ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
chmod -R 755 ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
sed -i '71s/git/git\ zsh-autosuggestions\ zsh-syntax-highlighting/g' ~/.zshrc
echo "安装已全部完成"
exec  zsh -l

